/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = ora_mar_engine.c                                   */
/*  NOTE      =                                                    */
/*  DATE      = 2014/09/26 by songbb                               */
/*******************************************************************/

#include <stdio.h>
#include <string.h>
#include <frlog.h>

#include "frcomm.h"
#include "frmem.h"
#include "frlist.h"

#include "../ora_core.h"
#include "../comm/ora_comm.h"
#include "../comm/ora_util.h"
#include "ora_mar_engine.h"
#include "ora_ttc_proto.h"

/* int类型数据最大值 */
#define APM_ORA_INT_MAX    2147483647
#define APM_ORA_FRSTR_DEFAULT_LEN 128

int ora_mar_check_bufflen(L7_ora_packet *p_st_ora_packet, unsigned long long ull_need_len)
{
    if (ull_need_len == 0LL)
        return FR_SUCC;
		
    if (p_st_ora_packet == NULL)
        return FR_FAIL;

    if (bytebuff_remaining(p_st_ora_packet->p_st_packet) < (int) ull_need_len)
    {
        ora_packet_need_next_packet(p_st_ora_packet);
        return FR_FAIL;
    }

    return FR_SUCC;
}

int ora_mar_check_bufflen_from(L7_ora_packet *p_st_ora_packet, unsigned int ui_from, unsigned long long ull_need_len)
{
    if (ull_need_len == 0LL)
        return FR_SUCC;

    if (p_st_ora_packet == NULL)
        return FR_FAIL;

    if ((int) (ui_from + ull_need_len) > bytebuff_length(p_st_ora_packet->p_st_packet))
    {
        ora_packet_need_next_packet(p_st_ora_packet);
        return FR_FAIL;
    }

    return FR_SUCC;
}

int ora_mar_skip(L7_ora_packet *p_st_ora_packet, unsigned long long ull_need_len)
{
    if (ull_need_len == 0LL)
        return FR_SUCC;

    if (p_st_ora_packet == NULL)
        return FR_FAIL;

    if (ora_mar_check_bufflen(p_st_ora_packet, ull_need_len) != FR_SUCC)
        return FR_FAIL;
    if (bytebuff_skip(p_st_ora_packet->p_st_packet, (size_t) ull_need_len) != FR_SUCC)
        return FR_FAIL;
    return FR_SUCC;
}

FR_STATIC int __ora_mar_detect_big_chunks(L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet)
{
    short s_tmp = 0;

    if(p_st_sess == NULL || p_st_ora_packet == NULL)
        return FR_FAIL;

    if (p_st_sess->i_use_special_field == FR_NO)
    {
        if (ora_mar_check_bufflen(p_st_ora_packet, 1) != FR_SUCC)
            return FR_FAIL;

        if (ora_mar_peekUB1(p_st_ora_packet, &s_tmp) != FR_SUCC)
            return FR_FAIL;

        p_st_sess->i_useCLR_big_chunks = s_tmp <= 4 ? FR_YES : FR_NO;
    }
    else if (p_st_sess->i_use_special_field == FR_YES)
    {
        int i_pos = bytebuff_getPosition(p_st_ora_packet->p_st_packet);
        short s1, s2, s3, s4;
        if (ora_mar_unmarshalUB1(p_st_ora_packet, &s1) != FR_SUCC || s1 == 0x00)
            return FR_FAIL;
        if (ora_mar_unmarshalUB1(p_st_ora_packet, &s2) != FR_SUCC)
            return FR_FAIL;
        if (ora_mar_unmarshalUB1(p_st_ora_packet, &s3) != FR_SUCC)
            return FR_FAIL;
        if (ora_mar_unmarshalUB1(p_st_ora_packet, &s4) != FR_SUCC)
            return FR_FAIL;
        if ((s2 == 0x00 && s3 == 0x00 && s4 == 0x00) || (s3 == 0x00 && s4 == 0x00))
            p_st_sess->i_useCLR_big_chunks = FR_YES;
        else
            p_st_sess->i_useCLR_big_chunks = FR_NO;
        bytebuff_setPosition(p_st_ora_packet->p_st_packet, i_pos);
    }
    else
        return FR_FAIL;

    return FR_SUCC;
}

FR_STATIC int __ora_mar_getDataFromBuffer(Frstr *p_outbuff, int i_len_to_read, L7_ora_packet *p_st_ora_packet, int *p_i_len_read)
{
    if (p_outbuff == NULL || p_st_ora_packet == NULL)
        return FR_FAIL;

    int i_buff_remaining = bytebuff_remaining(p_st_ora_packet->p_st_packet);
    if (i_buff_remaining < i_len_to_read || i_buff_remaining <= 0 || i_len_to_read <= 0)
    {
        ora_packet_need_next_packet(p_st_ora_packet);
        return FR_FAIL;
    }
    char *p_buff_tmp = NULL;

    if ((p_buff_tmp = (char *) frmalloc(i_len_to_read * sizeof(char))) == NULL)
    {
        ORA_MALLOCK_ERR;
        return FR_FAIL;
    }

    if (bytebuff_get_bytes(p_st_ora_packet->p_st_packet, p_buff_tmp, (unsigned int) i_len_to_read) != FR_SUCC)
        return FR_FAIL;

    frstr_append(p_outbuff, p_buff_tmp, (unsigned int) i_len_to_read);

    *p_i_len_read = i_len_to_read;

    ora_free((void **) &p_buff_tmp);
    return FR_SUCC;
}

FR_STATIC int __ora_mar_read1(L7_ora_packet *p_st_ora_packet, int *p_i_len_read)
{
    Frstr *p_tmpbuff = NULL;
    int i_len_read = 0;

    if (p_st_ora_packet == NULL)
        return FR_FAIL;

    if (ora_mar_check_bufflen(p_st_ora_packet, 1) != FR_SUCC)
        return FR_FAIL;

    if ((p_tmpbuff = frstr_new(1)) == NULL)
    {
        ORA_MALLOCK_ERR;
        return FR_FAIL;
    }

    if (__ora_mar_getDataFromBuffer(p_tmpbuff, 1, p_st_ora_packet, &i_len_read) != FR_SUCC)
    {
        frstr_free(&p_tmpbuff);
        return FR_FAIL;
    }

    *p_i_len_read = i_len_read < 0 ? -1 : p_tmpbuff->p_str[0] & 0xFF;
    frstr_free(&p_tmpbuff);

    return FR_SUCC;
}

FR_STATIC int __ora_mar_getNBytes(Frstr *p_outbuff, int i_len_to_read, L7_ora_packet *p_st_ora_packet, int *p_i_len_read)
{
    int i_len_read = 0;

    if(p_outbuff == NULL || p_st_ora_packet ==NULL)
        return FR_FAIL;

    if (ora_mar_check_bufflen(p_st_ora_packet, (unsigned long long) i_len_to_read) != FR_SUCC)
        return FR_FAIL;

    if (__ora_mar_getDataFromBuffer(p_outbuff, i_len_to_read, p_st_ora_packet, &i_len_read) != FR_SUCC || i_len_read < 0)
        return FR_FAIL;

    *p_i_len_read = i_len_read;

    return FR_SUCC;
}

FR_STATIC int __ora_mar_getNBytes_by_len(int i_len, L7_ora_packet *p_st_ora_packet, char **pp_outbuff)
{
    Frstr *p_tmpbuff = NULL;
    int i_read = 0;

    if (p_st_ora_packet == NULL)
        return FR_FAIL;

    if ((p_tmpbuff = frstr_new(APM_ORA_FRSTR_DEFAULT_LEN)) == NULL)
    {
        ORA_MALLOCK_ERR;
        return FR_FAIL;
    }

    if (__ora_mar_getDataFromBuffer(p_tmpbuff, i_len, p_st_ora_packet, &i_read) != FR_SUCC)
    {
        frstr_free(&p_tmpbuff);
        return FR_FAIL;
    }

    *pp_outbuff = frstr_free2(&p_tmpbuff);

    return FR_SUCC;
}

int ora_mar_unmarshalUB1(L7_ora_packet *p_st_ora_packet, short *p_s_out)
{
    int i_read = 0;

    if (p_st_ora_packet == NULL)
        return FR_FAIL;

    if (__ora_mar_read1(p_st_ora_packet, &i_read) != FR_SUCC)
        return FR_FAIL;
    *p_s_out = (short) i_read;
    return FR_SUCC;
}

int ora_mar_unmarshalSB1(L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet, char *p_out)
{
    short s_read = 0;

    if (p_st_ora_packet == NULL)
        return FR_FAIL;

    if (ora_mar_unmarshalSB2(p_st_sess, p_st_ora_packet, &s_read) != FR_SUCC)
        return FR_FAIL;

    *p_out = (char) s_read;

    return FR_SUCC;
}

int ora_mar_unmarshalUB2(L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet, int *p_i_out)
{
    long long ll_value = 0LL;

    if (p_st_ora_packet == NULL)
        return FR_FAIL;

    if (ora_mar_buffer2Value_12c_one(1, p_st_sess, p_st_ora_packet, &ll_value) != FR_SUCC)
        return FR_FAIL;

    *p_i_out = ((int) ll_value) & 0xFFFF;

    return FR_SUCC;
}

int ora_mar_unmarshalSB2(L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet, short *p_s_out)
{
    int i_read = 0;

    if (p_st_ora_packet == NULL)
        return FR_FAIL;

    if (ora_mar_unmarshalUB2(p_st_sess, p_st_ora_packet, &i_read) != FR_SUCC)
        return FR_FAIL;

    *p_s_out = (short) i_read;

    return FR_SUCC;
}

int ora_mar_unmarshalUB4(L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet, long long *p_ll_out)
{
    if (p_st_ora_packet == NULL)
        return FR_FAIL;

    return ora_mar_buffer2Value_12c_one(2, p_st_sess, p_st_ora_packet, p_ll_out);
}

int ora_mar_unmarshalSB4(L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet, int *p_i_out)
{
    long long ll_value = 0LL;

    if (p_st_ora_packet == NULL)
        return FR_FAIL;

    if (ora_mar_unmarshalUB4(p_st_sess, p_st_ora_packet, &ll_value) != FR_SUCC)
        return FR_FAIL;

    *p_i_out = (int) ll_value;

    return FR_SUCC;
}

int ora_mar_unmarshalSB8(L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet, long long *p_ll_out)
{
    if (p_st_ora_packet == NULL)
        return FR_FAIL;

    return ora_mar_buffer2Value_12c_one(3, p_st_sess, p_st_ora_packet, p_ll_out);
}

int ora_mar_unmarshalSWORD(L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet, int *p_i_out)
{
    long long ll_value = 0LL;

    if (p_st_ora_packet == NULL)
        return FR_FAIL;

    if (ora_mar_unmarshalUB4(p_st_sess, p_st_ora_packet, &ll_value) != FR_SUCC)
        return FR_FAIL;

    *p_i_out = (int) ll_value;

    return FR_SUCC;
}

int ora_mar_unmarshalUWORD(L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet, long long *p_ll_out)
{
    if (p_st_ora_packet == NULL)
        return FR_FAIL;

    return ora_mar_unmarshalUB4(p_st_sess, p_st_ora_packet, p_ll_out);
}

int ora_mar_unmarshalNBytes_by_len(int i_len, L7_ora_packet *p_st_ora_packet, char **pp_outbuff)
{
    if (p_st_ora_packet == NULL)
        return FR_FAIL;

    return __ora_mar_getNBytes_by_len(i_len, p_st_ora_packet, pp_outbuff);
}

int ora_mar_unmarshalNBytes(Frstr *p_outbuff, int i_len_to_read, L7_ora_packet *p_st_ora_packet, int *p_i_out)
{
    int i_all = 0;
    int i_one = 0;

    if(p_outbuff == NULL || p_st_ora_packet == NULL)
        return FR_FAIL;

    while (i_all < i_len_to_read)
    {
        if (__ora_mar_getNBytes(p_outbuff, i_len_to_read - i_all, p_st_ora_packet, &i_one) != FR_SUCC)
            return FR_FAIL;

        i_all += i_one;
    }

    *p_i_out = i_all;

    return FR_SUCC;
}

int ora_mar_escapeSequenceNull(int i_param, int *p_i_out)
{
    int i_ret = 0;
    switch (i_param)
    {
        case 0:
            i_ret = 1;
            break;
        case 253:
            return FR_FAIL;
        case 255:
            i_ret = 1;
            break;
        case 254:
            break;
    }
    *p_i_out = i_ret;
    return FR_SUCC;
}

int ora_mar_unmarshalBuffer(Frstr *p_outbuff, int i_param_size, int i_param1, int i_param2, L7_ora_packet *p_st_ora_packet, int *p_i_out)
{
    int i_byte = 0;
    Frstr *p_ignored = NULL;

    if(p_outbuff == NULL || p_st_ora_packet == NULL)
        return FR_FAIL;

    if ((p_ignored = frstr_new(APM_ORA_FRSTR_DEFAULT_LEN)) == NULL)
        return FR_FAIL;

    if (i_param2 <= 0)
    {
        *p_i_out = i_param1;
        frstr_free(&p_ignored);
        return FR_SUCC;
    }

    if (i_param_size < i_param1 + i_param2)
    {
        if (ora_mar_unmarshalNBytes(p_outbuff, i_param_size - i_param1, p_st_ora_packet, &i_byte) != FR_SUCC)
            goto error;
        if (ora_mar_unmarshalNBytes(p_ignored, i_param1 + i_param2 - i_param_size, p_st_ora_packet, &i_byte) != FR_SUCC)
            goto error;
        i_param1 = -1;
    }
    else
    {
        if (ora_mar_unmarshalNBytes(p_outbuff, i_param2, p_st_ora_packet, &i_byte) != FR_SUCC)
            goto error;
        i_param1 += i_param2;
    }
    *p_i_out = i_param1;

    frstr_free(&p_ignored);
    return FR_SUCC;
error:
    frstr_free(&p_ignored);
    return FR_FAIL;
}

int ora_mar_get_bytes(L7_ora_packet *p_st_ora_packet, unsigned int ui_len_to_read, char **pp_out)
{
    char *p_buff_tmp = NULL;

    if(p_st_ora_packet == NULL || ui_len_to_read == 0)
        return FR_FAIL;

    if (ora_mar_check_bufflen(p_st_ora_packet, ui_len_to_read) != FR_SUCC)
        return FR_FAIL;

    if ((p_buff_tmp = (char *) frmalloc(ui_len_to_read * sizeof(char))) == NULL)
    {
        ORA_MALLOCK_ERR;
        return FR_FAIL;
    }

    if (bytebuff_get_bytes(p_st_ora_packet->p_st_packet, p_buff_tmp, ui_len_to_read) != FR_SUCC)
        goto error;

    *pp_out = p_buff_tmp;

    return FR_SUCC;
error:
    ora_free((void **) &p_buff_tmp);
    return FR_FAIL;
}

int ora_mar_unmarshalCLR_ttc(L7_ora_packet *p_st_ora_packet, char **pp_out, int *p_i_out_len)
{
    ORA_PRINT_INFO("ora_mar_unmarshalCLR_ttc\n");
    int i_block_len = 0;
    int i_ret = 0;
    int i_is_first_block = FR_YES;
    Frstr *p_frstr = NULL;
    char *p_buff_tmp = NULL;

    if (p_st_ora_packet == NULL)
        return FR_FAIL;

    if (ora_mar_unmarshalUB1(p_st_ora_packet, (short *) &i_block_len) != FR_SUCC || i_block_len < 0)
        return FR_FAIL;

    if (i_block_len == 0)
    {
        *p_i_out_len = 0;
        *pp_out = NULL;
        return FR_SUCC;
    }

    if (ora_mar_escapeSequenceNull(i_block_len, &i_ret) != FR_SUCC)
        return FR_FAIL;

    if (i_ret)
    {
        *p_i_out_len = 0;
        *pp_out = NULL;
        return FR_SUCC;
    }
    if (i_block_len != 254)
    {
        if (ora_mar_get_bytes(p_st_ora_packet, (unsigned int) i_block_len, pp_out) != FR_SUCC)
            return FR_FAIL;
        *p_i_out_len = i_block_len;
    }
    else
    {
        for (; ;)
        {
            if (ora_mar_unmarshalUB1(p_st_ora_packet, (short *) &i_block_len) != FR_SUCC)
                goto error;
            if (i_block_len <= 0)
                break;

            if (i_is_first_block)
            {
                if ((p_frstr = frstr_new((unsigned int) i_block_len)) == NULL)
                    goto error;
                i_is_first_block = FR_NO;
            }

            if (ora_mar_get_bytes(p_st_ora_packet, (unsigned int) i_block_len, &p_buff_tmp) != FR_SUCC)
                goto error;

            if (frstr_append(p_frstr, p_buff_tmp, (unsigned int) i_block_len) != FR_SUCC)
                goto error;

            ora_free((void **) &p_buff_tmp);
        }

        *p_i_out_len = FRSTR_LEN(p_frstr);
        *pp_out = frstr_free2(&p_frstr);
    }

    ORA_PRINT_INFO("leave ora_mar_unmarshalCLR_ttc\n");
    return FR_SUCC;
error:
    ora_free((void **) &p_buff_tmp);
    frstr_free(&p_frstr);
    return FR_FAIL;
}

int ora_mar_unmarshalCLR(int i_param, int *p_arr_int, L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet, char **pp_out, int *p_i_out_len)
{
    int i_len_to_read = 0;
    int j = 0;
    int k = 0;
    int i_ret = 0;
    Frstr *p_tmpbuf = NULL;
    Frstr *p_ignored = NULL;
    *p_i_out_len = 0;

    if(p_st_sess == NULL || p_st_ora_packet == NULL)
        return FR_FAIL;

    if (p_st_sess->i_c2snlsratio <= 0)
    {
        ORA_PRINT_ERR("i_c2snlsratio is unknown\n");
        return FR_FAIL;
    }

    if (ora_mar_unmarshalUB1(p_st_ora_packet, (short *) &k) != FR_SUCC || k < 0)
        return FR_FAIL;

    if (k == 0)
    {
        p_arr_int[0] = 0;
        *pp_out = NULL;
        return FR_SUCC;
    }

    if (ora_mar_escapeSequenceNull(k, &i_ret) != FR_SUCC)
        goto error;

    if (i_ret)
    {
        p_arr_int[0] = 0;
        *pp_out = NULL;
        return FR_SUCC;
    }
    else
    {
        if ((p_tmpbuf = frstr_new(APM_ORA_FRSTR_DEFAULT_LEN)) == NULL)
        {
            ORA_MALLOCK_ERR;
            goto error;
        }

        if (k != 254)
        {
            if (ora_mar_unmarshalBuffer(p_tmpbuf, i_param * p_st_sess->i_c2snlsratio, j, k, p_st_ora_packet, &j) != FR_SUCC)
                goto error;
        }
        else
        {
            if (p_st_sess->i_useCLR_big_chunks == APM_ORA_FLAG_UNSET &&
                __ora_mar_detect_big_chunks(p_st_sess, p_st_ora_packet) != FR_SUCC)
            {
                ORA_PRINT_EMERG("unknown i_useCLR_big_chunks\n");
                ORA_FRLOG_ERROR("unknown i_useCLR_big_chunks");
                goto error;
            }
            if (p_st_sess->i_useCLR_big_chunks == FR_YES)
            {
                if (ora_mar_unmarshalSB4(p_st_sess, p_st_ora_packet, &i_len_to_read) != FR_SUCC)
                    goto error;
            }
            else
            {
                if (ora_mar_unmarshalUB1(p_st_ora_packet, (short *) &i_len_to_read) != FR_SUCC)
                    goto error;
            }
            if ((p_ignored = frstr_new(255)) == NULL)
                goto error;
            while (i_len_to_read > 0)
            {
                if (i_len_to_read != 254)
                {
                    if (j == -1)
                    {
                        int i_tmp = 0;
                        if (ora_mar_unmarshalBuffer(p_ignored, FRSTR_SIZE(p_ignored), 0, i_len_to_read, p_st_ora_packet, &i_tmp) != FR_SUCC)
                            goto error;
                    }
                    else
                    {
                        if (ora_mar_unmarshalBuffer(p_tmpbuf, i_param * p_st_sess->i_c2snlsratio, j, i_len_to_read, p_st_ora_packet, &j) != FR_SUCC)
                            goto error;
                        *p_i_out_len += i_len_to_read;
                    }
                }
                if (p_st_sess->i_useCLR_big_chunks == FR_YES)
                {
                    if (ora_mar_unmarshalSB4(p_st_sess, p_st_ora_packet, &i_len_to_read) != FR_SUCC)
                        goto error;
                }
                else
                {
                    if (ora_mar_unmarshalUB1(p_st_ora_packet, (short *) &i_len_to_read) != FR_SUCC)
                        goto error;
                }
            }
        }
        if (p_arr_int != NULL)
        {
            if (j != -1)
                p_arr_int[0] = j;
            else
                p_arr_int[0] = i_param * p_st_sess->i_c2snlsratio;
        }
        *p_i_out_len = FRSTR_LEN(p_tmpbuf);
        *pp_out = frstr_free2(&p_tmpbuf);
        frstr_free(&p_ignored);
    }

    return FR_SUCC;
error:
    frstr_free(&p_tmpbuf);
    frstr_free(&p_ignored);
    return FR_FAIL;
}

int ora_mar_unmarshalCLR_five(Frstr *p_outbuff, int i_param_size, long long ll_param, int *p_arr_int, int i_param1, int i_param2, L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet)
{
    int i = 0;
    int j = 0;
    long long l = ll_param;
    int k = 0;
    int m = 0;
    int n = 0;
    int i1 = 0;
    int i2 = -1;
    int i3;
    int i_ret = 0;
    Frstr *p_ignored = NULL;
    int i_tmp = 0;

    if(p_outbuff == NULL || p_st_sess == NULL || p_st_ora_packet == NULL)
        return FR_FAIL;

    if (ora_mar_unmarshalUB1(p_st_ora_packet, (short *) &i) != FR_SUCC || k < 0)
        return FR_FAIL;

    if (i == 0)
    {
        p_arr_int[0] = 0;
        return FR_SUCC;
    }

    if (ora_mar_escapeSequenceNull(i, &i_ret) != FR_SUCC)
    {
        return FR_FAIL;
    }
    if (i_ret)
    {
        p_arr_int[0] = 0;
    }
    else
    {
        if ((p_ignored = frstr_new(32767)) == NULL)
            goto error;
        if (i != 254)
        {
            if (i_param2 - i1 >= i)
            {
                if (ora_mar_unmarshalBuffer(p_ignored, FRSTR_SIZE(p_ignored), 0, i, p_st_ora_packet, &i_tmp) != FR_SUCC)
                    goto error;
                i1 += i;
                i = 0;
            }
            else if (i_param2 - i1 > 0)
            {
                if (ora_mar_unmarshalBuffer(p_ignored, FRSTR_SIZE(p_ignored), 0, i_param2 - i1, p_st_ora_packet, &i_tmp) != FR_SUCC)
                    goto error;
                i -= i_param2 - i1;
                i1 += i_param2 - i1;
            }
            if (i > 0)
            {
                n = (i_param1 - m) < i ? (i_param1 - m) : i;
                if (ora_mar_unmarshalBuffer(p_outbuff, i_param_size, (int) l, n, p_st_ora_packet, (int *) &l) != FR_SUCC)
                    goto error;
                m += n;
                i3 = i - n;
                if (i3 > 0)
                {
                    if (ora_mar_unmarshalBuffer(p_ignored, FRSTR_SIZE(p_ignored), 0, i3, p_st_ora_packet, &i_tmp) != FR_SUCC)
                        goto error;
                }
            }
        }
        else
        {
            i2 = -1;
            for (; ;)
            {
                if (i2 != -1)
                {
                    if (p_st_sess->s_tns_version >= 315)
                    {
                        if (p_st_sess->i_useCLR_big_chunks == APM_ORA_FLAG_UNSET &&
                            __ora_mar_detect_big_chunks(p_st_sess, p_st_ora_packet) != FR_SUCC)
                        {
                            ORA_PRINT_EMERG("unknown i_useCLR_big_chunks\n");
                            ORA_FRLOG_ERROR("unknown i_useCLR_big_chunks");
                            goto error;
                        }
                        if (p_st_sess->i_useCLR_big_chunks == FR_YES)
                        {
                            if (ora_mar_unmarshalSB4(p_st_sess, p_st_ora_packet, &i) != FR_SUCC)
                                goto error;
                        }
                        else
                        {
                            if (ora_mar_unmarshalUB1(p_st_ora_packet, (short *) &i) != FR_SUCC)
                                goto error;
                        }
                    }
                    else
                    {
                        if (ora_mar_unmarshalUB1(p_st_ora_packet, (short *) &i) != FR_SUCC)
                            goto error;
                    }
                    if (i <= 0)
                    {
                        break;
                    }
                }
                if (i == 254)
                {
                    switch (i2)
                    {
                        case -1:
                            i2 = 1;
                            break;
                        case 1:
                        case 0:
                            i2 = 0;
                            break;
                    }
                }
                else
                {
                    if (l == -1LL)
                    {
                        if (ora_mar_unmarshalBuffer(p_ignored, FRSTR_SIZE(p_ignored), 0, i, p_st_ora_packet, &i_tmp) != FR_SUCC)
                            goto error;
                    }
                    else
                    {
                        j = i;
                        if (i_param2 - i1 >= j)
                        {
                            if (ora_mar_unmarshalBuffer(p_ignored, FRSTR_SIZE(p_ignored), 0, j, p_st_ora_packet, &i_tmp) != FR_SUCC)
                                goto error;
                            i1 += j;
                            j = 0;
                        }
                        else if (i_param2 - i1 > 0)
                        {
                            if (ora_mar_unmarshalBuffer(p_ignored, FRSTR_SIZE(p_ignored), 0, i_param2 - i1, p_st_ora_packet, &i_tmp) != FR_SUCC)
                                goto error;
                            j -= i_param2 - i1;
                            i1 += i_param2 - i1;
                        }
                        if (j > 0)
                        {
                            n = (i_param1 - m) < j ? (i_param1 - m) : j;
                            if (ora_mar_unmarshalBuffer(p_outbuff, i_param_size, (int) l, n, p_st_ora_packet, (int *) &l) != FR_SUCC)
                                goto error;
                            m += n;
                            i3 = j - n;
                            if (i3 > 0)
                            {
                                if (ora_mar_unmarshalBuffer(p_ignored, FRSTR_SIZE(p_ignored), 0, i3, p_st_ora_packet, &i_tmp) != FR_SUCC)
                                    goto error;
                            }
                        }
                    }
                    i2 = 0;
                    if (i > 252)
                        k = 1;
                }
            }
        }

        if (p_arr_int != NULL)
        {
            if (l != -1LL)
                p_arr_int[0] = m;
            else
                p_arr_int[0] = i_param_size - (int) ll_param;
        }
    }

    frstr_free(&p_ignored);
    return FR_SUCC;
error:
    frstr_free(&p_ignored);
    return FR_FAIL;
}

int unmarshalCLR_four(Frstr *p_outbuff, int i_param_size, int i_param, int *p_arr_int, int i_param2, L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet)
{
    return ora_mar_unmarshalCLR_five(p_outbuff, i_param_size, (long long) i_param, p_arr_int, i_param2, 0, p_st_sess, p_st_ora_packet);
}

int unmarshalCLR_three(Frstr *p_outbuff, int i_param_size, int i_param, int *p_arr_int, L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet)
{
    return unmarshalCLR_four(p_outbuff, i_param_size, i_param, p_arr_int, APM_ORA_INT_MAX, p_st_sess, p_st_ora_packet);
}

int ora_mar_unmarshalCHR(int i_param, L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet, char **pp_out, int *p_i_out_len)
{
    char *p_res_buff = NULL;
    int i_tmp_len = 0;

    if(p_st_sess == NULL || p_st_ora_packet == NULL)
        return FR_FAIL;

    if (p_st_sess->i_CHR_conversion_flags == APM_ORA_FLAG_UNSET)
    {
        ORA_PRINT_ALERT("p_st_sess->i_CHR_conversion_flags == APM_ORA_FLAG_UNSET\n");
        return FR_FAIL;
    }
    else if (p_st_sess->i_CHR_conversion_flags == FR_YES) // (p_st_sess->c_conversion_flags & 0x2) > 0
    {
        int arr_i_tmp[1] = {0};
        if (ora_mar_unmarshalCLR(i_param, arr_i_tmp, p_st_sess, p_st_ora_packet, &p_res_buff, &i_tmp_len) != FR_SUCC)
            return FR_FAIL;
        *p_i_out_len = i_tmp_len;
    }
    else
    {
        if (__ora_mar_getNBytes_by_len(i_param, p_st_ora_packet, &p_res_buff) != FR_SUCC)
            return FR_FAIL;
        *p_i_out_len = i_param;
    }
    *pp_out = p_res_buff;

    return FR_SUCC;
}

int ora_mar_unmarshalDALC(L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet, char **pp_out, int *p_i_out_len)
{
    long long l_byte = 0;
    int i_malloc = 0;
    char *p_tmp = NULL;
    int arr_i_tmp[1] = {0};
    int i_tmp_len = 0;

    if(p_st_sess == NULL || p_st_ora_packet == NULL)
    	return FR_FAIL;

    if (ora_mar_unmarshalUB4(p_st_sess, p_st_ora_packet, &l_byte) != FR_SUCC)
        return FR_FAIL;

    i_malloc = (int) (0xFFFFFFFF & l_byte);
    if (i_malloc <= 0)
    {
        *pp_out = NULL;
        return FR_SUCC;
    }

    if (ora_mar_unmarshalCLR(i_malloc, arr_i_tmp, p_st_sess, p_st_ora_packet, &p_tmp, &i_tmp_len) != FR_SUCC)
        return FR_FAIL;

    *p_i_out_len = i_tmp_len;
    *pp_out = p_tmp;

    return FR_SUCC;
}

int ora_mar_unmarshalCLRforREFS(L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet, char **pp_out, int *p_i_out_len)
{
    int i = 0;
    int j = 0;
    int k = 0;
    int i_ret = 0;
    Frstr *p_tmpbuff = NULL;
    int i_tmp = 0;

    if (p_st_ora_packet == NULL || p_st_sess == NULL)
        return FR_FAIL;

    if (ora_mar_unmarshalUB1(p_st_ora_packet, (short *) &k) != FR_SUCC || k < 0)
        goto error;

    if (k == 0)
    {
        *pp_out = NULL;
        goto success;
    }
    if (ora_mar_escapeSequenceNull(k, &i_ret) != FR_SUCC)
        goto error;

    if (i_ret)
    {
        *pp_out = NULL;
        goto success;
    }

    if ((p_tmpbuff = frstr_new(APM_ORA_FRSTR_DEFAULT_LEN)) == NULL)
    {
        ORA_MALLOCK_ERR;
        goto error;
    }

    if (k == 254)
    {
        if (p_st_sess->i_useCLR_big_chunks == APM_ORA_FLAG_UNSET &&
            __ora_mar_detect_big_chunks(p_st_sess, p_st_ora_packet) != FR_SUCC)
        {
            ORA_PRINT_EMERG("unknown i_useCLR_big_chunks\n");
            ORA_FRLOG_ERROR("unknown i_useCLR_big_chunks");
            goto error;
        }
        if (p_st_sess->i_useCLR_big_chunks == FR_YES)
        {
            if (ora_mar_unmarshalSB4(p_st_sess, p_st_ora_packet, &i) != FR_SUCC || k < 0)
                goto error;
        }
        else
        {
            if (ora_mar_unmarshalUB1(p_st_ora_packet, (short *) &i) != FR_SUCC || k < 0)
                goto error;
        }

        while (i > 0)
        {
            if (i != 254 || p_st_sess->i_srv_conversion == FR_NO)
            {
                j = (short) (j + i);
                if (ora_mar_check_bufflen(p_st_ora_packet, (unsigned long long) i) != FR_SUCC)
                    goto error;

                if (ora_mar_unmarshalBuffer(p_tmpbuff, i, 0, i, p_st_ora_packet, &i_tmp) != FR_SUCC)
                    goto error;
            }
            if (p_st_sess->i_useCLR_big_chunks == FR_YES)
            {
                if (ora_mar_unmarshalSB4(p_st_sess, p_st_ora_packet, &i) != FR_SUCC || k < 0)
                    goto error;
            }
            else
            {
                if (ora_mar_unmarshalUB1(p_st_ora_packet, (short *) &i) != FR_SUCC || k < 0)
                    goto error;
            }
        }
    }
    else
    {
        if (ora_mar_check_bufflen(p_st_ora_packet, (unsigned long long) k) != FR_SUCC)
            goto error;

        if (ora_mar_unmarshalBuffer(p_tmpbuff, k, 0, k, p_st_ora_packet, &i_tmp) != FR_SUCC)
            goto error;
    }

    *p_i_out_len = FRSTR_LEN(p_tmpbuff);
    *pp_out = frstr_free2(&p_tmpbuff);

success:
    return FR_SUCC;
error:
    frstr_free(&p_tmpbuff);
    return FR_FAIL;
}

int ora_mar_unmarshalO2U(L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet, char *p_out)
{
    short s_firstbyte = 0;
    long long ll_tmp = 0LL;
    uint64_t uint64_tmp1 = 0LL;
    uint64_t uint64_tmp2 = 0LL;

    if(p_st_sess == NULL || p_st_ora_packet == NULL)
        return FR_FAIL;

    if (ora_mar_unmarshalUB1(p_st_ora_packet, &s_firstbyte) != FR_SUCC)
        return FR_FAIL;

    if (p_st_sess->i_use_oci == FR_NO)
        *p_out = (char) s_firstbyte;
    else
    {
        if (p_st_sess->i_ptr_len == APM_ORA_PTR_LEN_1)
            *p_out = (char) s_firstbyte;
        else if (p_st_sess->i_ptr_len == APM_ORA_PTR_LEN_4)
        {
            if (bytebuffer_backspace(p_st_ora_packet->p_st_packet, 1) != FR_SUCC)
                return FR_FAIL;

            if (ora_mar_unmarshalUB4(p_st_sess, p_st_ora_packet, &ll_tmp) != FR_SUCC)
                return FR_FAIL;

            if (ll_tmp == 0LL)
                *p_out = 0;
            else
                *p_out = 1;
        }
        else if (p_st_sess->i_ptr_len == APM_ORA_PTR_LEN_8)
        {
            if (bytebuffer_backspace(p_st_ora_packet->p_st_packet, 1) != FR_SUCC)
                return FR_FAIL;

            if (ora_mar_check_bufflen(p_st_ora_packet, 8) != FR_SUCC)
                return FR_FAIL;

            if (bytebuff_get_long(p_st_ora_packet->p_st_packet, 4, &uint64_tmp1, BYTE_BIG_ENDIAN) != FR_SUCC)
                return FR_FAIL;
            if (bytebuff_get_long(p_st_ora_packet->p_st_packet, 4, &uint64_tmp2, BYTE_BIG_ENDIAN) != FR_SUCC)
                return FR_FAIL;

            if (uint64_tmp1 == 0LL && uint64_tmp2 == 0LL)
                *p_out = 0;
            else if ((uint64_tmp1 == 0xfeffffff && uint64_tmp2 == 0xffffffff) ||
                    (uint64_tmp1 == 0xffffffff && uint64_tmp2 == 0xfffffffe) ||
                    (uint64_tmp1 > 0LL && uint64_tmp2 == 0LL) ||
                    (uint64_tmp1 > 0LL && uint64_tmp2 > 0LL))
                *p_out = 1;
            else
                return FR_FAIL;
        }
        else
            return FR_FAIL;
    }

    return FR_SUCC;
}

int ora_mar_unmarshalB1Array(int i_param, L7_ora_packet *p_st_ora_packet, char **pp_out)
{
    return ora_mar_unmarshalNBytes_by_len(i_param, p_st_ora_packet, pp_out);
}

int ora_mar_unmarshalKEYVAL(int i_param, L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet)
{
    /**
    * 因为apm获取数据并未需要这里的key,value值，所以并未传出去
    */
    int i_loop = 0;
    int i_len = 0;
    Frstr *p_tmpbuff = NULL;
    int p_arr_int[1] = {0};
    int i_tmp = 0;

    if (p_st_ora_packet == NULL || p_st_sess == NULL)
        return FR_FAIL;

    if ((p_tmpbuff = frstr_new(1000)) == NULL)
        goto error;

    for (i_loop = 0; i_loop < i_param; i_loop++)
    {
        if (ora_mar_unmarshalSB4(p_st_sess, p_st_ora_packet, &i_len) == FR_FAIL)
            goto error;
        if (i_len > 0)
        {
            if (unmarshalCLR_three(p_tmpbuff, FRSTR_SIZE(p_tmpbuff), 0, p_arr_int, p_st_sess, p_st_ora_packet) != FR_SUCC)
                goto error;
        }
        if (ora_mar_unmarshalSB4(p_st_sess, p_st_ora_packet, &i_len) == FR_FAIL)
            goto error;
        if (i_len > 0)
        {
            if (unmarshalCLR_three(p_tmpbuff, FRSTR_SIZE(p_tmpbuff), 0, p_arr_int, p_st_sess, p_st_ora_packet) != FR_SUCC)
                goto error;
        }
        if (ora_mar_unmarshalSB4(p_st_sess, p_st_ora_packet, &i_tmp) == FR_FAIL)
            goto error;
    }

    frstr_free(&p_tmpbuff);
    return FR_SUCC;
error:
    frstr_free(&p_tmpbuff);
    return FR_FAIL;
}

int ora_mar_unmarshalKeywordValue(L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet)
{
    int i_i = 0;
    int i_j = 0;
    int i_k = 0;
    Frstr *p_tmpbuff = NULL;
    long long ll_tmp = 0LL;
    int p_arr_int[1] = {0};

    if(p_st_sess == NULL || p_st_ora_packet == NULL)
        return FR_FAIL;

    if (p_st_sess->i_use_oci == FR_YES)
    {
        if (ora_mar_unmarshalUB4(p_st_sess, p_st_ora_packet, &ll_tmp) != FR_SUCC) // i
            goto error;
        i_i = (int) ll_tmp;
    }
    else
    {
        if (ora_mar_unmarshalUB2(p_st_sess, p_st_ora_packet, &i_i) != FR_SUCC) // i
            goto error;
    }
    if (i_i != 0)
    {
        if ((p_tmpbuff = frstr_new(APM_ORA_FRSTR_DEFAULT_LEN)) == NULL)
        {
            ORA_MALLOCK_ERR;
            goto error;
        }
        if (unmarshalCLR_three(p_tmpbuff, i_i * sizeof(char), 0, p_arr_int, p_st_sess, p_st_ora_packet) == FR_SUCC)
            frstr_free(&p_tmpbuff);
        else
            goto error;
    }

    if (p_st_sess->i_use_oci == FR_YES)
    {
        if (ora_mar_unmarshalUB4(p_st_sess, p_st_ora_packet, &ll_tmp) != FR_SUCC) // j
            goto error;
        i_j = (int) ll_tmp;
    }
    else
    {
        if (ora_mar_unmarshalUB2(p_st_sess, p_st_ora_packet, &i_j) != FR_SUCC) // j
            goto error;
    }
    if (i_j != 0)
    {
        if ((p_tmpbuff = frstr_new(APM_ORA_FRSTR_DEFAULT_LEN)) == NULL)
        {
            ORA_MALLOCK_ERR;
            goto error;
        }
        if (unmarshalCLR_three(p_tmpbuff, i_j * sizeof(char), 0, p_arr_int, p_st_sess, p_st_ora_packet) == FR_SUCC)
            frstr_free(&p_tmpbuff);
        else
            goto error;
    }

    if (ora_mar_unmarshalUB2(p_st_sess, p_st_ora_packet, &i_k) != FR_SUCC) // k
        goto error;

    return FR_SUCC;
error:
    frstr_free(&p_tmpbuff);
    return FR_FAIL;
}

int ora_mar_unmarshalUB4Array(int i_arr_len, L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet)
{
    int i_loop = 0;

    if (p_st_ora_packet == NULL || p_st_sess == NULL)
        return FR_FAIL;

    for (i_loop = 0; i_loop < i_arr_len; i_loop++)
    {
        if (ora_mar_skipUB4(p_st_sess, p_st_ora_packet) != FR_SUCC)
            goto error;
    }

    return FR_SUCC;
error:
    return FR_FAIL;
}

/**
* 对应于12c的oracle.net.ns.DataPacket的public long readLongLSB(int paramInt)方法
*/
int ora_mar_readLongLSB_12c_pkt(int i_len_to_read, L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet, long long *p_ll_out)
{
    long long l = 0LL;
    int i = i_len_to_read;
    int j;
    uint8_t byte;

    if (p_st_ora_packet == NULL || p_st_sess == NULL)
        return FR_FAIL;

    for (j = 0; i > 0;)
    {
        if (bytebuff_get_char(p_st_ora_packet->p_st_packet, &byte) != FR_SUCC)
            return FR_FAIL;
        l |= (byte & 0xFF) << 8 * j;
        i--;
        j++;
    }
    *p_ll_out = l;

    return FR_SUCC;
}

/**
* 对应于12c的oracle.net.ns.DataPacket的public long readLongMSB(int paramInt)方法
*/
int ora_mar_readLongMSB_12c_pkt(int i_len_to_read, L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet, long long *p_ll_out)
{
    long long l = 0LL;
    int i;
    uint8_t byte;

    if (p_st_ora_packet == NULL || p_st_sess == NULL)
        return FR_FAIL;

    for (i = i_len_to_read; i > 0;)
    {
        if (bytebuff_get_char(p_st_ora_packet->p_st_packet, &byte) != FR_SUCC)
            return FR_FAIL;
        l |= (byte & 0xFF) << 8 * (i - 1);
        i--;
    }
    *p_ll_out = l;

    return FR_SUCC;
}

/**
* 对应于12c的oracle.net.ns.NetInputStream的public long readLongLSB(int paramInt)方法
*/
int ora_mar_readLongLSB_12c(int paramInt, L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet, long long *p_ll_out)
{
    long long l = 0LL;
    int i = 0;

    if (p_st_ora_packet == NULL || p_st_sess == NULL)
        return FR_FAIL;

    if ((paramInt & 0x80) > 0)
    {
        paramInt &= 0x7F;
        i = 1;
    }

    if (ora_mar_check_bufflen(p_st_ora_packet, (unsigned long long) paramInt) != FR_SUCC)
        return FR_FAIL;

    if (ora_mar_readLongLSB_12c_pkt(paramInt, p_st_sess, p_st_ora_packet, &l) != FR_SUCC)
        return FR_FAIL;

    *p_ll_out = (i != 0 ? -1 : 1) * l;

    return FR_SUCC;
}

/**
* 对应于12c的oracle.net.ns.NetInputStream的public long readLongMSB(int paramInt)方法
*/
int ora_mar_readLongMSB_12c(int paramInt, L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet, long long *p_ll_out)
{
    long long l = 0LL;
    int i = 0;

    if (p_st_ora_packet == NULL || p_st_sess == NULL)
        return FR_FAIL;

    if ((paramInt & 0x80) > 0)
    {
        paramInt &= 0x7F;
        i = 1;
    }

    if (ora_mar_check_bufflen(p_st_ora_packet, (unsigned long long) paramInt) != FR_SUCC)
        return FR_FAIL;

    if (ora_mar_readLongMSB_12c_pkt(paramInt, p_st_sess, p_st_ora_packet, &l) != FR_SUCC)
        return FR_FAIL;

    *p_ll_out = (i != 0 ? -1 : 1) * l;

    return FR_SUCC;
}

/**
* 对应于12c的oracle.jdbc.driver.T4CMAREngineStream的final long buffer2Value(byte paramByte)方法
*/
int ora_mar_buffer2Value_12c_one(int i_param, L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet, long long *p_ll_out)
{
    long long l = 0LL;
    int i = 1;

    if (p_st_ora_packet == NULL || p_st_sess == NULL)
        return FR_FAIL;

    if ((p_st_sess->arr_ttc_type_rep[i_param] & 0x1) > 0)
    {
        if (__ora_mar_read1(p_st_ora_packet, &i) != FR_SUCC)
            goto error;
        if ((i_param == 1 && i > 2 && i != 0x81) || (i_param == 2 && i > 4 && i != 0x81) || (i_param == 3 && i > 8 && i != 0x81))
            goto error;
    }
    else
    {
        switch (i_param)
        {
            case 1:
                i = 2;
                break;
            case 2:
                i = 4;
                break;
            case 3:
                i = 8;
                break;
            default:
                goto error;
        }
    }
    if ((p_st_sess->arr_ttc_type_rep[i_param] & 0x2) > 0)
    {
        if (ora_mar_readLongLSB_12c(i, p_st_sess, p_st_ora_packet, &l) != FR_SUCC)
            goto error;
    }
    else
    {
        if (ora_mar_readLongMSB_12c(i, p_st_sess, p_st_ora_packet, &l) != FR_SUCC)
            goto error;
    }
    *p_ll_out = l;

    return FR_SUCC;
error:
    return FR_FAIL;
}

int ora_mar_unmarshalUCS2(L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet, int *p_i_out)
{
    int i_tmp = 0;
    char c_tmpbuffer_0 = 0;
    char c_tmpbuffer_1 = 0;

    if (p_st_ora_packet == NULL || p_st_sess == NULL)
        return FR_FAIL;

    if (ora_mar_unmarshalUB2(p_st_sess, p_st_ora_packet, &i_tmp) != FR_SUCC)
        return FR_FAIL;

    c_tmpbuffer_0 = (char) (i_tmp & 0xFF00) >> 8;
    c_tmpbuffer_1 = (char) (i_tmp & 0xFF);

    *p_i_out = c_tmpbuffer_0 == 0 ? 2 : c_tmpbuffer_1 == 0 ? 1 : 3;

    return FR_SUCC;
}


/**************************************** SKIP *****************************************/

FR_STATIC int __ora_mar_skipBuffer(L7_ora_packet *p_st_ora_packet, long long ll_skiplen)
{
    if (ll_skiplen == 0LL)
        return FR_SUCC;

    if (p_st_ora_packet == NULL)
        return FR_FAIL;

    if (ora_mar_skip(p_st_ora_packet, (size_t) ll_skiplen) != FR_SUCC)
        return FR_FAIL;

    return FR_SUCC;
}

int ora_mar_skipUB1(L7_ora_packet *p_st_ora_packet)
{
    return __ora_mar_skipBuffer(p_st_ora_packet, 1);
}

int ora_mar_skipO2U(L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet)
{
    uint64_t uint64_tmp1 = 0LL;
    uint64_t uint64_tmp2 = 0LL;

    if (p_st_sess == NULL || p_st_ora_packet == NULL)
        return FR_FAIL;

    if (p_st_sess->i_use_oci == FR_NO)
        return __ora_mar_skipBuffer(p_st_ora_packet, 1);
    else
    {
        if (p_st_sess->i_ptr_len == APM_ORA_PTR_LEN_1)
            return __ora_mar_skipBuffer(p_st_ora_packet, 1);
        else if (p_st_sess->i_ptr_len == APM_ORA_PTR_LEN_4)
        {
            return ora_mar_skipUB4(p_st_sess, p_st_ora_packet);
        }
        else if (p_st_sess->i_ptr_len == APM_ORA_PTR_LEN_8)
        {
            if (ora_mar_check_bufflen(p_st_ora_packet, 8) != FR_SUCC)
                return FR_FAIL;

            if (bytebuff_get_long(p_st_ora_packet->p_st_packet, 4, &uint64_tmp1, BYTE_BIG_ENDIAN) != FR_SUCC)
                return FR_FAIL;
            if (bytebuff_get_long(p_st_ora_packet->p_st_packet, 4, &uint64_tmp2, BYTE_BIG_ENDIAN) != FR_SUCC)
                return FR_FAIL;

            if (uint64_tmp1 == 0LL && uint64_tmp2 == 0LL)
            {
            }
            else if ((uint64_tmp1 == 0xfeffffff && uint64_tmp2 == 0xffffffff) ||
                    (uint64_tmp1 == 0xffffffff && uint64_tmp2 == 0xfffffffe) ||
                    (uint64_tmp1 > 0LL && uint64_tmp2 == 0LL))
            {
            }
            else
                return FR_FAIL;
        }
        else
            return FR_FAIL;
    }

    return FR_SUCC;
}

int ora_mar_skipUB2(L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet)
{
    if (p_st_ora_packet == NULL)
        return FR_FAIL;

    return ora_mar_skipbuffer2Value_12c_one(1, p_st_sess, p_st_ora_packet);
}

int ora_mar_skipSB2(L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet)
{
    return ora_mar_skipUB2(p_st_sess, p_st_ora_packet);
}

int ora_mar_skipUB4(L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet)
{
    if (p_st_ora_packet == NULL)
        return FR_FAIL;

    return ora_mar_skipbuffer2Value_12c_one(2, p_st_sess, p_st_ora_packet);
}

int ora_mar_skipSB4(L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet)
{
    return ora_mar_skipUB4(p_st_sess, p_st_ora_packet);
}

int ora_mar_skipNBytes_by_len(int i_len, L7_ora_packet *p_st_ora_packet)
{
    return __ora_mar_skipBuffer(p_st_ora_packet, i_len);
}

int ora_mar_skipB1Array(int i_param, L7_ora_packet *p_st_ora_packet)
{
    return __ora_mar_skipBuffer(p_st_ora_packet, i_param);
}

int ora_mar_skipNBytes(int i_len_to_read, L7_ora_packet *p_st_ora_packet)
{
    return __ora_mar_skipBuffer(p_st_ora_packet, i_len_to_read);
}

int ora_mar_skipTEXT(int i_len, L7_ora_packet *p_st_ora_packet)
{
    int i_loop = 0;
    short s_tmp = 0;

    if (p_st_ora_packet == NULL)
        return FR_FAIL;

    while (i_loop < i_len)
    {
        if (ora_mar_unmarshalUB1(p_st_ora_packet, &s_tmp) != FR_SUCC)
            goto error;

        i_loop++;
        if (s_tmp == 0)
            break;
    }

    return FR_SUCC;
error:
    return FR_FAIL;
}

int ora_mar_skipCLR(L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet)
{
    int i = 0;
    int k = 0;
    int i_ret = 0;

    if (p_st_ora_packet == NULL || p_st_sess == NULL)
        return FR_FAIL;

    if (ora_mar_unmarshalUB1(p_st_ora_packet, (short *) &k) != FR_SUCC || k < 0)
        return FR_FAIL;

    if (k == 0)
    {
        return FR_SUCC;
    }
    if (ora_mar_escapeSequenceNull(k, &i_ret) != FR_SUCC)
    {
        return FR_FAIL;
    }
    if (!i_ret)
    {
        if (k != 254)
        {
            if (__ora_mar_skipBuffer(p_st_ora_packet, k) != FR_SUCC)
                goto error;
        }
        else
        {
            if (p_st_sess->i_useCLR_big_chunks == APM_ORA_FLAG_UNSET &&
                __ora_mar_detect_big_chunks(p_st_sess, p_st_ora_packet) != FR_SUCC)
            {
                ORA_PRINT_EMERG("unknown i_useCLR_big_chunks\n");
                ORA_FRLOG_ERROR("unknown i_useCLR_big_chunks");
                goto error;
            }
            if (p_st_sess->i_useCLR_big_chunks == FR_YES)
            {
                if (ora_mar_unmarshalSB4(p_st_sess, p_st_ora_packet, &i) != FR_SUCC)
                    goto error;
            }
            else
            {
                if (ora_mar_unmarshalUB1(p_st_ora_packet, (short *) &i) != FR_SUCC)
                    goto error;
            }
            while (i > 0)
            {
                if (i != 254)
                {
                    if (__ora_mar_skipBuffer(p_st_ora_packet, i) != FR_SUCC)
                        goto error;
                }
                if (p_st_sess->i_useCLR_big_chunks == FR_YES)
                {
                    if (ora_mar_unmarshalSB4(p_st_sess, p_st_ora_packet, &i) != FR_SUCC)
                        goto error;
                }
                else
                {
                    if (ora_mar_unmarshalUB1(p_st_ora_packet, (short *) &i) != FR_SUCC)
                        goto error;
                }
            }
        }
    }

    return FR_SUCC;
error:
    return FR_FAIL;
}

int ora_mar_skipCHR(int i_param, L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet)
{
    if (p_st_ora_packet == NULL || p_st_sess == NULL)
        return FR_FAIL;

    if (p_st_sess->i_CHR_conversion_flags == APM_ORA_FLAG_UNSET)
    {
        ORA_PRINT_ALERT("p_st_sess->i_CHR_conversion_flags == APM_ORA_FLAG_UNSET\n");
        return FR_FAIL;
    }
    else if (p_st_sess->i_CHR_conversion_flags == FR_YES) // (p_st_sess->c_conversion_flags & 0x2) > 0
    {
        if (ora_mar_skipCLR(p_st_sess, p_st_ora_packet) != FR_SUCC)
            return FR_FAIL;
    }
    else
    {
        if (ora_mar_skipNBytes_by_len(i_param, p_st_ora_packet) != FR_SUCC)
            return FR_FAIL;
    }

    return FR_SUCC;
}

int ora_mar_skipDALC(L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet)
{
    long long l_byte = 0;
    int i_malloc = 0;

    if (p_st_ora_packet == NULL || p_st_sess == NULL)
        return FR_FAIL;

    if (ora_mar_unmarshalUB4(p_st_sess, p_st_ora_packet, &l_byte) != FR_SUCC)
        return FR_FAIL;

    i_malloc = (int) (0xFFFFFFFF & l_byte);
    if (i_malloc <= 0)
    {
        return FR_SUCC;
    }

    if (ora_mar_skipCLR(p_st_sess, p_st_ora_packet) != FR_SUCC)
        return FR_FAIL;

    return FR_SUCC;
}

int ora_mar_skipbuffer2Value_12c_one(int i_param, L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet)
{
    int i = 1;

    if (p_st_ora_packet == NULL || p_st_sess == NULL)
        return FR_FAIL;

    if ((p_st_sess->arr_ttc_type_rep[i_param] & 0x1) > 0)
    {
        if (__ora_mar_read1(p_st_ora_packet, &i) != FR_SUCC)
            goto error;
        if ((i_param == 1 && i > 2 && i != 0x81) || (i_param == 2 && i > 4 && i != 0x81) || (i_param == 3 && i > 8 && i != 0x81))
            goto error;
    }
    else
    {
        switch (i_param)
        {
            case 1:
                i = 2;
                break;
            case 2:
                i = 4;
                break;
            case 3:
                i = 8;
                break;
            default:
                goto error;
        }
    }

    return __ora_mar_skipBuffer(p_st_ora_packet, i);

    return FR_SUCC;
error:
    return FR_FAIL;
}

int ora_mar_skipCLRLen(L7session_ora *p_st_sess, L7_ora_packet *p_st_ora_packet, int *p_i_isnull)
{
    int i = 0;
    int k = 0;
    int i_ret = 0;
    *p_i_isnull = FR_NO;

    if (p_st_ora_packet == NULL || p_st_sess == NULL)
        return FR_FAIL;

    if (ora_mar_unmarshalUB1(p_st_ora_packet, (short *) &k) != FR_SUCC || k < 0)
        return FR_FAIL;

    if (k == 0)
    {
        *p_i_isnull = FR_YES;
        return FR_SUCC;
    }
    if (ora_mar_escapeSequenceNull(k, &i_ret) != FR_SUCC)
    {
        return FR_FAIL;
    }
    if (!i_ret)
    {
        if (k != 254)
        {
            if (__ora_mar_skipBuffer(p_st_ora_packet, k) != FR_SUCC)
                goto error;
        }
        else
        {
            if (p_st_sess->i_useCLR_big_chunks == APM_ORA_FLAG_UNSET &&
                __ora_mar_detect_big_chunks(p_st_sess, p_st_ora_packet) != FR_SUCC)
            {
                ORA_PRINT_EMERG("unknown i_useCLR_big_chunks\n");
                ORA_FRLOG_ERROR("unknown i_useCLR_big_chunks");
                goto error;
            }
            if (p_st_sess->i_useCLR_big_chunks == FR_YES)
            {
                if (ora_mar_unmarshalSB4(p_st_sess, p_st_ora_packet, &i) != FR_SUCC)
                    goto error;
            }
            else
            {
                if (ora_mar_unmarshalUB1(p_st_ora_packet, (short *) &i) != FR_SUCC)
                    goto error;
            }
            while (i > 0)
            {
                if (i != 254)
                {
                    if (__ora_mar_skipBuffer(p_st_ora_packet, i) != FR_SUCC)
                        goto error;
                }
                if (p_st_sess->i_useCLR_big_chunks == FR_YES)
                {
                    if (ora_mar_unmarshalSB4(p_st_sess, p_st_ora_packet, &i) != FR_SUCC)
                        goto error;
                }
                else
                {
                    if (ora_mar_unmarshalUB1(p_st_ora_packet, (short *) &i) != FR_SUCC)
                        goto error;
                }
            }
        }
    }
    else
        *p_i_isnull = FR_YES;

    return FR_SUCC;
error:
    return FR_FAIL;
}

int ora_mar_peekUB1(L7_ora_packet *p_st_ora_packet, short *p_s_out)
{
    if (p_st_ora_packet == NULL)
        return FR_FAIL;

    if (ora_mar_check_bufflen(p_st_ora_packet, 1) != FR_SUCC)
        return FR_FAIL;
    return bytebuff_peek_short(p_st_ora_packet->p_st_packet, 1, (unsigned short *) p_s_out, BYTE_BIG_ENDIAN);
}

int ora_mar_peekUB2(L7_ora_packet *p_st_ora_packet, short *p_s_out)
{
    if (p_st_ora_packet == NULL)
        return FR_FAIL;

    if (ora_mar_check_bufflen(p_st_ora_packet, 2) != FR_SUCC)
        return FR_FAIL;
    return bytebuff_peek_short(p_st_ora_packet->p_st_packet, 2, (unsigned short *) p_s_out, BYTE_BIG_ENDIAN);
}
